package com.example.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;

@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    // 当 beanFactory被实例化好后（bean创建之前），回调这个函数，注册一些BeanDefinition
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        // 注册一个 Teacher 的 BeanDefinition
        GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
        beanDefinition.setBeanClass(Teacher.class);

        // 向下强转
        DefaultListableBeanFactory factory = (DefaultListableBeanFactory) beanFactory;
        factory.registerBeanDefinition("teacher", beanDefinition);
    }

    /*
            实现类名                                             作用
            ConfigurationClassPostProcessor                     扫描启动类所在包下的注解
            ServletComponentRegisteringPostProcessor            扫描 @WebServlet、@WebFilter、@WebListener
            CachingMetadataReaderFactoryPostProcessor           配置ConfigurationClassPostProcessor
            ConfigurationWarningPostProcessor                   配置警告提示
     */
}
